package com.quanghoa.nguphaptienganh;
import java.sql.SQLException;
import java.util.ArrayList;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
//import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.util.Log;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.widget.TextView;
public class ireVerb extends Activity implements OnItemClickListener {

	
private DataBaseHelper myDbHelper;
private ArrayList<String>arraywork;
private ArrayAdapter<String> arrayadapter;
private ListView list;
private String TAG ="ireVerb";


@Override
public void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.iregverb);
    	loadListView();
    	list.setTextFilterEnabled(true);
    	//list.setOnListItemClickListener(this);
    	//list.setOnClickListener(null);
    	list.setOnItemClickListener(this);
    	
    		
    	
    }
private void loadListView()
{

	arraywork = new ArrayList<String>();
	arrayadapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,arraywork);
	list = (ListView)findViewById(R.id.listIregVerb);
	list.setAdapter(arrayadapter);
	myDbHelper = new DataBaseHelper(this);
	try{
		myDbHelper.checkAndCopyDatabase();
		myDbHelper.openDataBase();
		
	}catch(SQLException sqle){
		Log.d(TAG,"database error!");
	}
	try{
		Cursor c = myDbHelper.QueryData("select Verb from IreVerb ");
		if(c!= null){
			if(c.moveToFirst()){
				int i =0;
				do{
					String str = c.getString(c.getColumnIndex("Verb"));
					arraywork.add(i++,str);
				}while(c.moveToNext());
			}
			arrayadapter.notifyDataSetChanged();
		}
	} catch (SQLException e){
			Log.d(TAG,"Query errror!");
	}
}
// Show Toast Message
    private void showMessage(String text){
    	Context context = getApplicationContext();
    	int duration = Toast.LENGTH_SHORT;
    	Toast toast = Toast.makeText(context, "Bạn Chọn Động Từ : " +text, duration);
    	toast.show();
    	
    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, Menu.FIRST, 0, "Exit").setIcon(android.R.drawable.ic_delete);
    	return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	finish();
    	return super.onOptionsItemSelected(item);
    }
	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
		// TODO Auto-generated method stub
			// Show by Toast method showMessage(string text)
		String s = (String) list.getItemAtPosition(position);
			showMessage(s);
			
			
			// Show by Dialog
    		AlertDialog.Builder adb=new AlertDialog.Builder(ireVerb.this);
    		adb.setTitle("Động Từ Bất Quy Tắc");
    		adb.setMessage("Bạn chọn động từ : "+list.getItemAtPosition(position));
    		adb.setPositiveButton("Ok", null);
    		adb.show();
    		
    		
	}
}